package com.exception;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @auther: zhangxin
 * @Description:
 */
@ControllerAdvice
public class MyGlobalExceptionHandler {

    @ExceptionHandler(NoHandlerFoundException.class)
    public String handle(Exception ex){
        System.out.println("...............................404............");
        return "404";//404是视图名
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ModelAndView customException(MethodArgumentNotValidException e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", e.getMessage());
        mv.setViewName("error");
        return mv;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView customException(Exception e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", e.getMessage());
        mv.setViewName("error");
        return mv;
    }


}
